﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using KegData;

namespace KegManager
{
    [ValueConversion(typeof(double), typeof(double))]
    public sealed class KegVolumeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((double)value / 18.927059) * 230;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(double), typeof(double))]
    public sealed class KegPintsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double UnitsPerPint = 0.56826125; // Number of liters in a pint

            return System.Math.Floor((double)value / UnitsPerPint);
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(double), typeof(ImageSource))]
    public sealed class BeerColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Uri colorUri = null;
            BeerColor color = (BeerColor)value;
            
            if (color == BeerColor.Straw)
            {
                colorUri = new Uri("pack://application:,,,/Images/beerstraw.png");
            }
            else if (color == BeerColor.Copper)
            {
                colorUri = new Uri("pack://application:,,,/Images/beercopper.png");
            }
            else if (color == BeerColor.Red)
            {
                colorUri = new Uri("pack://application:,,,/Images/beerred.png");
            }
            else if (color == BeerColor.Brown)
            {
                colorUri = new Uri("pack://application:,,,/Images/beerbrown.png");
            }
            else if (color == BeerColor.Black)
            {
                colorUri = new Uri("pack://application:,,,/Images/beerblack.png");
            }
            else
            {
                colorUri = new Uri("pack://application:,,,/Images/beeryellow.png");
            }
          
            if (colorUri != null)
            {
                return new BitmapImage(colorUri);
            }
            else
            {
                return null;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Interaction logic for KegControl.xaml
    /// </summary>
    public partial class KegControl : UserControl
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(KegControl));
        public static readonly DependencyProperty PintsProperty = DependencyProperty.Register("ShowPints", typeof(Visibility), typeof(KegControl));
        public static readonly DependencyProperty FillOpacityProperty = DependencyProperty.Register("FillOpacity", typeof(double), typeof(KegControl), new PropertyMetadata(0.5));
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(BeerColor), typeof(KegControl), new PropertyMetadata(BeerColor.Yellow));

        public KegControl()
        {
            InitializeComponent();
        }

        public double Value
        {
            get
            {
                return (double)GetValue(KegControl.ValueProperty);
            }
            set
            {
                SetValue(KegControl.ValueProperty, value);
            }
        }

        public Visibility ShowPints
        {
            get
            {
                return (Visibility)GetValue(KegControl.PintsProperty);
            }
            set
            {
                SetValue(KegControl.PintsProperty, value);
            }
        }

        public double FillOpacity
        {
            get
            {
                return (double)GetValue(KegControl.FillOpacityProperty);
            }
            set
            {
                SetValue(KegControl.FillOpacityProperty, value);
            }
        }

        public BeerColor Color
        {
            get
            {
                return (BeerColor)GetValue(KegControl.ColorProperty);
            }
            set
            {
                SetValue(KegControl.ColorProperty, value);
            }
        }
    }
}
